// api/mock_adapter.dart
import 'dart:convert';
import 'package:dio/dio.dart';
import 'dart:math';

class MockAdapter extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    final random = Random();

    // Mock Login
    // if (options.path == 'api/app/user/login') {
    //   handler.resolve(Response(
    //       requestOptions: options,
    //       statusCode: 200,
    //       // *** MOCK DATA NOW INCLUDES A TOKEN ***
    //       data: {
    //         "id": 1,
    //         "code": 200,
    //         "message": "章鱼哥",
    //         "success": true,
    //         "data": {
    //           "id": 5,
    //           "gmtCreate": 1755510709000,
    //           "gmtModified": 1755510714000,
    //           "phone": options.queryParameters['phone'] ?? '16605102260',
    //           "reference": null,
    //           "referenceCompany": null,
    //           "registerTime": "2025-08-18",
    //           "companyId": null,
    //           "appCompanyDto": null,
    //           "aiEndTime": null,
    //           "manualConsultEndTime": null,
    //           "url": null,
    //           "adminId": null,
    //           "companyName": null,
    //           "token": "a-very-secret-mock-token-that-persists",
    //         },
    //       }));
    //   return;
    // }


    // Mock Home Summary
    if (options.path == '/home/summary') {
      final year = options.queryParameters['year'] ?? 2025;
      final factor = (year - 2022) * 0.5; // Data changes based on year

      handler.resolve(Response(
        requestOptions: options,
        statusCode: 200,
        data: {
          "companyName": "章鱼哥科技有限公司",
          "year": year,
          "totalProfit": 123456.78 * factor,
          "totalIncome": 876543.21 * factor,
          "totalExpense": 753086.43 * factor,
          "otherItems": [
            {"title": "应收账款", "amount": 52013.14 * factor},
            {"title": "应付账款", "amount": 23456.78 * factor},
            {"title": "销项税额", "amount": 11234.56 * factor},
            {"title": "进项税额", "amount": 8765.43 * factor},
            {"title": "实缴税金", "amount": 2469.13 * factor},
          ],
          "invoiceData": {
            "salesCount": (83 * factor).toInt(),
            "salesAmount": 54088921.12 * factor,
            "purchaseCount": (65 * factor).toInt(),
            "purchaseAmount": 48123456.78 * factor
          }
        },
      ));
      return;
    }

    // *** NEW: Mock Report Data ***
    if (options.path == '/reports/data') {
      final year = options.queryParameters['year'] ?? 2025;
      final factor = (year - 2022) * 0.5 + random.nextDouble() * 0.2;

      handler.resolve(Response(
        requestOptions: options,
        statusCode: 200,
        data: {
          "year": year,
          "profitTrend": [
            {"label": "第一季度", "value": 2.5 * factor + random.nextDouble()},
            {"label": "第二季度", "value": 4.1 * factor + random.nextDouble()},
            {"label": "第三季度", "value": 8.3 * factor + random.nextDouble()},
            {"label": "第四季度", "value": 11.2 * factor + random.nextDouble()},
          ],
          "taxTrend": [
            {"label": "第一季度", "value": 1.8 * factor + random.nextDouble()},
            {"label": "第二季度", "value": 3.2 * factor + random.nextDouble()},
            {"label": "第三季度", "value": 5.0 * factor + random.nextDouble()},
            {"label": "第四季度", "value": 7.9 * factor + random.nextDouble()},
          ]
        },
      ));
      return;
    }

    // Fallback for unhandled requests
    handler.next(options);
  }
}
